interval = 0
time = 0

$("#sendCaptcha").click(e => {
    $("#sendCaptcha").addClass("am-disabled")
    
    sendEmail({email: $("#email")[0].value}, "reset", d => {
        time = 9
        $("#sendCaptcha").text("冷却：" + (time + 1) + "s")
        interval = setInterval(() => {
            if(time == 0) {
                $("#sendCaptcha").text("获取验证码")
                $("#sendCaptcha").removeClass("am-disabled")
                clearInterval(interval)
                return;
            }
            $("#sendCaptcha").text("冷却：" + time + "s")
            --time
        }, 1000)
    }, d => {
        $("#sendCaptcha").removeClass("am-disabled")
        alert("验证码发送失败\n" + d.msg)
    })
})

$("#btn-reg").click(e => {
    if($("#password")[0].value.length < 8) {
        alert("密码长度至少为 8 位")
        return;
    }
    data = []
    data.push($("#email")[0].value)
    data.push($("#captcha")[0].value)

    var hashSecret = CryptoJS.HmacSHA256($("#password")[0].value, keySecret);
    data.push(CryptoJS.enc.Hex.stringify(hashSecret))

    postForgot(data, e => {
        alert("重置成功，将开始登录")
        let loginData = {email: data[0], pwd_token: CryptoJS.enc.Hex.stringify(hashSecret)}
        postLogin(JSON.stringify(loginData), e => {
            alert("登录成功")
            localStorage.setItem("token", e)
            window.location.href = 'contest.html'
        }, e => {
            alert("登录失败\n" + e.msg)
        })
    }, e => alert(e.msg))
})